module view {

    export class FarmLandView extends eui.Component {
        closeBtn: eui.Button
        bagBtn: eui.Button
        storeBtn: eui.Button
        itemsGroup: eui.Group
        selectPack: number
        landGroup: eui.Group
        public constructor() {
            super()
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/FarmLandSkin.exml"
            this.name = 'FarmLandView'
        }

        private onComplete(): void {
            this.bagBtn['value'] = gplayer.bag.type
            this.storeBtn['value'] = gplayer.store.type
            this.bagBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.packageTouch,this)
            this.storeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.packageTouch,this)
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.showItems(gplayer.bag)
            this.showFarmLand()
        }

        private packageTouch(evt: egret.TouchEvent): void {
            var target = evt.target
            var packagetype = target.value
            if(packagetype === gplayer.bag.type) {
                this.showItems(gplayer.bag)
            } else {
                this.showItems(gplayer.store)
            }
        }

        public showFarmLand() {
            this.landGroup.removeChildren()
            var farm: game.FarmLand = gplayer.farmLand
            for(var i = 0;i < farm.num;i++) {
                var land = farm.lands[i]
                var btn = new eui.Button()
                btn.skinName = "resource/control/Button4Skin.exml"
                this.landGroup.addChild(btn)
                if(land) {
                    var itemConfig = totalConfig[land.itemId]
                    var text = ''
                    var time = gplayer.basicInfo.time - land.startTime
                    if(itemConfig.growTime > time) {
                        text = '剩余时间:' + Math.ceil((itemConfig.growTime - time)/4) + '小时'
                    } else {
                        text = '成熟'
                    }
                    btn.label = templateName[land.itemId] + '\n状况:' + land.value + '\n' + text                   
                }
            }
        }

        private showItems(pack: game.Package): void {
            this.itemsGroup.removeChildren()
            for(var i in pack.items) {
                var item = pack.items[i]
                if(!item || Math.floor(item.id / 100) !== 2002) {
                    continue
                }
                var btn: eui.Button = new eui.Button()
                btn.skinName = "resource/control/Button3Skin.exml"
                btn.width = 160
                this.itemsGroup.addChild(btn)
                var itemId = item.id
                var stack = item.stack
                btn.label = templateName[itemId] + 'X' + stack
                btn['value'] = {
                    packageType: pack.type,
                    index: i
                }
                btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.openView,this)
            }
        }

        private openView(evt: egret.TouchEvent): void {
            var target = evt.target
            var value = target.value
            var view = new FarmLandPopSkin(value)
            this.addChild(view)
        }
        
        private close(): void {
            this.parent.removeChild(this)
        }
    }

    class FarmLandPopSkin extends eui.Component {
        nameLabel: eui.Label
        contentLabel: eui.Label
        confirmBtn: eui.Label
        cancelBtn: eui.Label
        info: any
        public constructor(info) {
            super()
            this.skinName = "resource/skin/FarmLandPopSkin.exml"
            this.cancelBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.confirmBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.plant,this)
            this.info = info
            this.show()
        }

        private close() {
            this.parent.removeChild(this)
        }

        private plant(evt: egret.TouchEvent) {
            var farm: game.FarmLand = gplayer.farmLand
            for(var i = 0;i < farm.num;i++) {
                var land = farm.lands[i]
                if(land) {
                    continue
                }
                var packageType = this.info.packageType
                var index = this.info.index
                var pack: game.Package
                if(packageType === gplayer.bag.type) {
                    pack = gplayer.bag
                } else {
                    pack = gplayer.store
                }
                var item = pack.items[index]
                farm.lands[i] = new game.Land({
                    itemId: item.id,
                    startTime: gplayer.basicInfo.time
                })
                break
            }
            this.stage.getChildByName('FarmLandView')['showFarmLand']()
            this.parent.removeChild(this)
        }

        public show() {
            var packageType = this.info.packageType
            var index = this.info.index
            var pack: game.Package
            if(packageType === gplayer.bag.type) {
                pack = gplayer.bag
            } else {
                pack = gplayer.store
            }
            var item = pack.items[index]
            var itemConfig = totalConfig[item.id]
            this.nameLabel.text = itemConfig.name
            this.contentLabel.text = '介绍说明'
        }
    }
}
